/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package supermarket;

import java.io.File;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import model.Customer;
import model.Purchase;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author 
 */
public class PurchaseXMLManager {
        
    private final DateFormat df;
    private File xmlFile;
    private boolean isNewDocument = true;
    private Integer failedTries;
    CustomerThread cuThread;
    Boolean completed;
    ArrayList<Timestamp> timestampList = new ArrayList<>();
    
    
    
    public PurchaseXMLManager(File xmlFile,Integer failedTries,CustomerThread cuThread,Boolean completed
    ,ArrayList<Timestamp> timestampList){
        this.xmlFile = xmlFile;        
        this.failedTries=failedTries;
        this.cuThread=cuThread;
        this.completed=completed;
       this.timestampList=timestampList;
        
        
        df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    }
    
    private Document readXmlDocument() {
        Document document;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(xmlFile);
            isNewDocument = false;
            return document;
        }catch (Exception e){
            // File does not exist
            isNewDocument = true;
            return null;
        }
    }
    
    public void writeXML(List<Purchase> purchases){        
        Document document = readXmlDocument();
        
        
        try {            
            if (isNewDocument){ // Create New file
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                document = docBuilder.newDocument();            
                Element rootElement = document.createElement("purchases");            
                document.appendChild(rootElement);
            }
            Element root = document.getDocumentElement();
            
            for (Purchase p:purchases){
                // purchase elements                
                Element purchase = document.createElement("Purchase");
                root.appendChild(purchase);

                // set attribute to purchase element
                Attr attr = document.createAttribute("id");
                attr.setValue(p.getPurchaseId().toString());
                purchase.setAttributeNode(attr);

                // date elements
                Element date = document.createElement("date");
                date.appendChild(document.createTextNode(df.format(p.getDatetime())));
                purchase.appendChild(date);
               
                // amount elements
                Element amount = document.createElement("amount");
                amount.appendChild(document.createTextNode(Float.toString(p.getAmount())));
                purchase.appendChild(amount);
                
                // points_earned element
                Element pointsEarned = document.createElement("points_earned");
                pointsEarned.appendChild(document.createTextNode(Integer.toString(p.getPointsEarned())));
                purchase.appendChild(pointsEarned);
                
                // delivery elements
                Element isCreditCardUsed = document.createElement("delivery");
                isCreditCardUsed.appendChild(document.createTextNode(Boolean.toString((boolean) p.getDelivery())));
                purchase.appendChild(isCreditCardUsed);

                // store elements
                Element store = document.createElement("store");
                store.appendChild(document.createTextNode(p.getStore().getName()));
                purchase.appendChild(store);

                // customer elements
                Element customer = document.createElement("customer");
                customer.appendChild(document.createTextNode(p.getCustomer().getLastName()+" "+p.getCustomer().getFirstName()));
                purchase.appendChild(customer);
               
                 // thread elements
                Element thread = document.createElement("thread");
                thread.appendChild(document.createTextNode(this.cuThread.getName()));
                purchase.appendChild(thread);
                  
                // tries elements
                Element tries = document.createElement("tries");
                tries.appendChild(document.createTextNode(failedTries.toString()));
                purchase.appendChild(tries);
                
               
                try
                {
                
               // timestamp elements 1st try
                if(timestampList.get(0)!=null)   
                {
                Element timestamp1 = document.createElement("first_try");
                timestamp1.appendChild(document.createTextNode(timestampList.get(0).toString()));
                purchase.appendChild(timestamp1);
                }
                
                // timestamp elements 2nd try
                if(timestampList.get(1)!=null)   
                {
                Element timestamp2 = document.createElement("second_try");
                timestamp2.appendChild(document.createTextNode(timestampList.get(1).toString()));
                purchase.appendChild(timestamp2);
                }
                
                
                
                // timestamp elements 3rd try
                if(timestampList.get(2)!=null)   
                {
                Element timestamp3 = document.createElement("third_try");
                timestamp3.appendChild(document.createTextNode(timestampList.get(2).toString()));
                purchase.appendChild(timestamp3);
                }
                }
                
                catch(Exception e)
                {
                    
                }
                
                 // completed elements
                Element completed = document.createElement("completed");
                if(this.completed==true)
                completed.appendChild(document.createTextNode("OK"));
                if(this.completed==false)
                completed.appendChild(document.createTextNode("NOK"));
                purchase.appendChild(completed);
                
            }
            
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            DOMSource source = new DOMSource(document);      
            StreamResult result = new StreamResult(xmlFile);
            transformer.transform(source, result);
            //System.out.println("            File saved!");
            
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    // Δεν θα χρειαστεί ο υπολογισμός του ΜΟ σ' αυτή την εργασία
    public float calculateAveragePurchaseAmount(){
        Document document = readXmlDocument();        
        if (isNewDocument) {
            System.out.println("Δεν υπάρχει το αρχείο purchases.xml στην τοποθεσία C:\\temp");
            return 0.0f;
        }
        
        Element root = document.getDocumentElement();
        NodeList nodeList = root.getChildNodes();
        float sum=0.0f;
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node currentNode = nodeList.item(i);            
            NodeList subNodeList = currentNode.getChildNodes();
            for (int j = 0; j < subNodeList.getLength(); j++) {
                Node currentSubNode = subNodeList.item(j);
                if (currentSubNode.getNodeName().equalsIgnoreCase("amount")) {
                    float value = Float.valueOf(currentSubNode.getTextContent());
                    sum+=value;
                }                
            }            
        }
        return sum/(float)nodeList.getLength();
    }
}

